import bge,mathutils,math,random
from time import time
from ..common import *
from ..inputs import *
from .item import UsableItem
from .control import HeldItem

class Weapon(UsableItem):
	
	_abilities_ = {
		'selected':'Nothing'
	}
	
	def __init__(self):
		UsableItem.__init__(self)
		self.damage = 0

class FireWeapon(Weapon):
	
	_abilities_ = {
		'reloading':'Nothing'
	}
	
	def __init__(self):
		Weapon.__init__(self)
		self.ammo = 0
		self.inClip = 0

class HeldWeapon(HeldItem):
	
	useMethod = ()
	
	class defaultNulWeapon(HeldItem.defaultNulItem):
		def selected(s,self,item):
			return
	
	def __init__(self,
				inpMap,
				basic_item = None,
				firstUse = bge.events.LEFTMOUSE,
				secondUse = bge.events.RIGHTMOUSE, 
				thirdUse = bge.events.MIDDLEMOUSE):
		motherOb = None
		basic_item = HeldWeapon.checkItem(basic_item,HeldWeapon.defaultNulWeapon(),True)
		if not issubclass(type(inpMap),InputMap):
			motherOb = inpMap
			inpMap = motherOb.settings['inputsMap']
		Twin.__init__(self, motherOb,{'inputsMap':inpMap,
						'default_weapon':basic_item,
						'weapon_input':[Input(inpMap,firstUse).addAct(self.first_use),
						Input(inpMap,secondUse).addAct(self.second_use),
						Input(inpMap,thirdUse).addAct(self.third_use)]},
					{'primary_weapon':basic_item})
		self.checkFunc()
		self.activity = [False,False,False]
		self.stats['primary_weapon'].taken(self)
	@classmethod
	def checkItem(cls, weapon, error_return = False, it_return = False):
		if not issubclass(type(weapon),Weapon) or \
		 not all(hasattr(weapon,a) for a in ('first','second','third','taken','selected')):
			return error_return
		if it_return:
			return weapon
		return True
		
	def checkFunc(self):
		for aMethod in self.__class__.useMethod:
			if not hasattr(self.stats['primary_weapon'],aMethod):
				setattr(self,aMethod+'Func',self.doNothing)
			else:
				setattr(self,aMethod+'Func',getattr(self,aMethod))
		
	def first_use(self,active):
		self.activity[0] = bool(active)
		self.stats['primary_weapon'].first(self,active)
	
	def second_use(self,active):
		self.activity[1] = bool(active)
		self.stats['primary_weapon'].second(self,active)
	
	def third_use(self,active):
		self.activity[2] = bool(active)
		self.stats['primary_weapon'].third(self,active)
		
	def change_item(self,item):
		if HeldItem.checkItem(item):
			if self.activity[0]:
				self.first_use(0.0)
			if self.activity[1]:
				self.second_use(0.0)
			if self.activity[2]:
				self.third_use(0.0)
			self.stats['primary_weapon'].selected(self,item)
			self.stats['primary_weapon'] = item
			self.checkFunc()
			self.stats['primary_weapon'].taken(self)
			
